
import type { useDataViewStore } from "@/stores/dataview";
import type { CreateComponentType } from "@/views/dataview/packages/public/type";
import { ref, toRefs, watch } from "vue";
import type VChart from 'vue-echarts'
import { useChartDataPondFetch } from "./useChartDataPondFetch";
import { intervalUnitHandle, isPreview } from "@/utils";
import { ChartFrameEnum } from "@/views/dataview/packages/enum";
import { setOptions } from "@/views/dataview/packages/public";
import { RequestDataTypeEnum } from "@/const";

type ChartEditStoreType = typeof useDataViewStore

// 监听数据更改设置eachrts
export const useChartDataFetch = (
    targetComponent:CreateComponentType,
    useDataViewStore:ChartEditStoreType,
    updateCallBack?:(...args:any)=>any)=>
{
    const vChartRef = ref<typeof VChart | null>()
    let fetchInterval = 0

    // 添加的数据池
    const {addGlobalDataInterface} = useChartDataPondFetch()

    // 组件分类
    const {chartFrame} = targetComponent.chartConfig

    // echarts组合Vchart库更新方式
    const echartsUpdateHandle = (dataset:any)=>{
        if(chartFrame === ChartFrameEnum.ECHARTS){
            if(vChartRef.value){
                setOptions(vChartRef.value,{dataset:dataset},false)
            }
        }
    }

    const requestIntervalFn = ()=>{
        const dataViewStore = useDataViewStore()
        // 全局配置
        const {requestOriginUrl,requestIntervalUnit:globalUnit,requestInterval:globalRequestInterval} = toRefs(dataViewStore.getRequestGlobalConfig)
        // 目标组件
        const {requestDataType,requestUrl,requestIntervalUnit:targetUnit,requestInterval:targetInterval} = toRefs(targetComponent.request)

        if(requestDataType.value != RequestDataTypeEnum.AJAX) return //非请求类型
        try{
            if(requestUrl?.value){
                // 自定义请求
                // requestOriginUrl可为空
                const completePath = requestOriginUrl?.value && requestOriginUrl.value  + requestUrl.value
                if(!completePath) return
                clearInterval(fetchInterval)
                const fetchFn = async ()=>{
                    console.log("自定义请求触发");
                }
                watch(()=>targetComponent.request.requestParams,()=>{
                    fetchFn()
                },{
                    immediate:true,
                    deep:true
                })

                const time = targetInterval?.value ? targetInterval.value : globalRequestInterval.value
                const unit = targetUnit.value ? targetUnit.value : globalUnit.value
                // 轮询
                if(time){
                    fetchInterval = setInterval(fetchFn,intervalUnitHandle(time,unit))
                }
            }
        }catch{

        }
    }

    if(isPreview()){
        targetComponent.request.requestDataType === RequestDataTypeEnum.Pond
        ? addGlobalDataInterface(targetComponent,useDataViewStore,(newData:any)=>{
            echartsUpdateHandle(newData)
            if(updateCallBack) updateCallBack(newData)
        })
        : requestIntervalFn()
    }else{
        requestIntervalFn()
    }

    return {
        vChartRef
    }
}